import store from '../store/index.js'

export const http = (method, url, data, _header) => {
	let _token = store.getters.getToken;
	let header = {
		Authorization: _token,
		"Content-Type": "application/x-www-form-urlencoded",
	};
	if (_header) {
		header = Object.assign({}, header, _header);
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			data: data,
			method,
			header,
			success(res) {
				if (res.statusCode == 200) {
					resolve(res.data)
				} else if (res.statusCode == 401 && res.data.error == "invalid_token") {
					uni.navigateTo({
						url: '/pages/user/login'
					})
				}
				console.log(res)
				if (res.data) {
					resolve(res.data)
				} else {
					resolve(res.data)
				}
			},
			fail(res) {
				resolve(res.data)
				console.log('请求失败', res.data);
			}
		});
	});
};